package edu.nf.takeoutdemo.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import edu.nf.takeoutdemo.commons.constant.StatusConstant;
import edu.nf.takeoutdemo.commons.result.PageResult;
import edu.nf.takeoutdemo.mapper.DishMapper;
import edu.nf.takeoutdemo.mapper.SetmealDishMapper;
import edu.nf.takeoutdemo.mapper.SetmealMapper;
import edu.nf.takeoutdemo.model.converter.SetmealConverter;
import edu.nf.takeoutdemo.model.dto.SetmealDTO;
import edu.nf.takeoutdemo.model.dto.SetmealPageQueryDTO;
import edu.nf.takeoutdemo.model.entity.Dish;
import edu.nf.takeoutdemo.model.entity.Setmeal;
import edu.nf.takeoutdemo.model.entity.SetmealDish;
import edu.nf.takeoutdemo.model.vo.DishItemVO;
import edu.nf.takeoutdemo.model.vo.SetmealVO;
import edu.nf.takeoutdemo.service.SetmealService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author zjl
 * @date 2025/10/28
 */
@Service
@RequiredArgsConstructor
public class SetmealServiceImpl implements SetmealService {
    private final SetmealMapper setmealMapper;
    private final SetmealConverter  setmealConverter;
    private final SetmealDishMapper  setmealDishMapper;
    private final DishMapper  dishMapper;

    @Cacheable(cacheNames = "setmealCache",key = "#setmeal.categoryId")
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> setmealList = setmealMapper.list(setmeal);
        return setmealList;
    }

    @Override
    public List<DishItemVO> getDishItemById(Long id) {
        List<DishItemVO> dishItemVOList =  setmealMapper.getDishItemById(id);
        return dishItemVOList;
    }

    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    @Transactional
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        Setmeal setmeal = setmealConverter.toSetmeal(setmealDTO);
        setmealMapper.add(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.addBatch(setmealDishes);
    }

    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPageNUm(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @Transactional
    @Override
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id -> {
            Setmeal setmeal = setmealMapper.getById(id);
            if(StatusConstant.ENABLE == setmeal.getStatus()){
                //起售中的套餐不能删除
                throw new RuntimeException("起售中的套餐不能删除");
            }
        });
            //删除套餐表中的数据
            setmealMapper.deleteByIds(ids);
            //删除套餐菜品关系表中的数据
            setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public SetmealVO getByIdWithDish(Long id) {
        Setmeal setmeal = setmealMapper.getById(id);
        List<SetmealDish> setmealDishVOList = setmealDishMapper.getBySetmealId(id);
        SetmealVO setmealVO = setmealConverter.toSetmealVO(setmeal);
        setmealVO.setSetmealDishes(setmealDishVOList);
        return setmealVO;
    }

    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = setmealConverter.toSetmeal(setmealDTO);
        setmealMapper.update(setmeal);
        Long setmealId = setmealDTO.getId();
        setmealDishMapper.deleteBySetmealId(setmealId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        setmealDishMapper.addBatch(setmealDishes);
    }

    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if(dishList!=null &&!dishList.isEmpty()){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE == dish.getStatus()){
                        throw new RuntimeException("套餐内包含未启售菜品，无法启售");
                    }
                });
            }
        }
        Setmeal setmeal = new Setmeal();
        setmeal.setStatus(status);
        setmeal.setId(id);
        setmealMapper.update(setmeal);
    }
}
